package com.lunasoft.nettyprotobuf;

import io.netty.channel.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jierry
 * @version 2016/7/13.
 */
public class SubReqClientHandler  extends SimpleChannelInboundHandler<SubscribeReqProto.SubscribeReq> {

//    @Override
//    public void channelActive(ChannelHandlerContext ctx) throws Exception {
////        for (int i = 0; i < 10; i++) {
////            ctx.write(req(i));
////        }
////        ctx.flush();
//
//          ChannelFuture f = ctx.writeAndFlush(req(1));
 //   f.addListener(new ChannelFutureListener() {
//                public void operationComplete(ChannelFuture channelFuture) throws Exception {
//                    System.out.print("===========write message success=========="+"\r\n");
//                }
//            });
//    }

    private SubscribeReqProto.SubscribeReq req(int i){
        SubscribeReqProto.SubscribeReq.Builder r = SubscribeReqProto.SubscribeReq.newBuilder();
        r.setSubReqID(i);
        r.setProductName("Netty Book"+i);
        r.setUserName("leeka");

        List<String> address = new ArrayList<String>();
        address.add("Nanjing");
        r.addAllAddress(address);
        address.add("Beijing");
        return r.build();
    }

//    @Override
//    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        SubscribeReqProto.SubscribeReq req = (SubscribeReqProto.SubscribeReq)msg;
//        ChannelFuture f = ctx.writeAndFlush(req(1));
//            f.addListener(new ChannelFutureListener() {
//                public void operationComplete(ChannelFuture channelFuture) throws Exception {
//                    System.out.print("===========write message success=========="+"\r\n");
//                }
//            });
////        while (true) {
////
////            ChannelFuture f = ctx.writeAndFlush(req(1));
////            f.addListener(new ChannelFutureListener() {
////                public void operationComplete(ChannelFuture channelFuture) throws Exception {
////                    System.out.print("===========write message success=========="+"\r\n");
////                }
////            });
//            System.out.println("receive server response:["+msg+"]");
//            //TimeUnit.SECONDS.sleep(5);
//        //}
//
//    }

    protected void channelRead0(ChannelHandlerContext channelHandlerContext, SubscribeReqProto.SubscribeReq subscribeReq) throws Exception {
        //SubscribeReqProto.SubscribeReq req = (SubscribeReqProto.SubscribeReq)msg;
        ChannelFuture f = channelHandlerContext.writeAndFlush(subscribeReq);
        f.addListener(new ChannelFutureListener() {
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                System.out.print("===========write message success=========="+"\r\n");
            }
        });
//        while (true) {
//
//            ChannelFuture f = ctx.writeAndFlush(req(1));
//            f.addListener(new ChannelFutureListener() {
//                public void operationComplete(ChannelFuture channelFuture) throws Exception {
//                    System.out.print("===========write message success=========="+"\r\n");
//                }
//            });
        System.out.println("receive server response:["+subscribeReq+"]");
        //TimeUnit.SECONDS.sleep(5);
        //}
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println(cause.getMessage());
        ctx.close();
    }
}
